package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.entity.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import com.cy.pj.sys.vo.CheckBox;
import com.cy.pj.sys.vo.SysRoleMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class SysRoleServiceimpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;


    @Override
    public PageObject<Object> findPageObjecys(String name, Integer pageCurrent) {
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确，页码值应该>=1");
        int rowCount=sysRoleDao.getRowCount(name);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");
        int pageSize = 2;
        int satrtIndex = (pageCurrent-1)*pageSize;
        List<SysRole> records = sysRoleDao.findPageObjects(name, satrtIndex,pageSize);

        return new PageObject(rowCount, records,pageSize, pageCurrent);
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        if(entity==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("角色名不允许为空");
        if(menuIds==null||menuIds.length==0)
            throw new ServiceException("必须为角色分配权限");
        int rows=sysRoleDao.insertObjec(entity);
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public SysRoleMenuVo findObjectById(Integer id) {
        if(id==null||id<=0)
            throw new IllegalArgumentException("id的值不合法");
        SysRoleMenuVo result=sysRoleDao.findObjectById(id);
        if(result==null)
            throw new ServiceException("此记录已经不存在");
        return result;
    }

    @Override
    public int updateObject(SysRole entity, Integer[] menuIds) {
//1.合法性验证
        if(entity==null)
            throw new IllegalArgumentException("更新的对象不能为空");
        if(entity.getId()==null)
            throw new IllegalArgumentException("id 的值不能为空");

        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("角色名不能为空");
        if(menuIds==null||menuIds.length==0)
            throw new IllegalArgumentException("必须为角色指定一个权限");

        //2.更新数据
        int rows=sysRoleDao.updateObject(entity);
       if(rows==0)
           throw new ServiceException("对象可能已经不存在");
/*        sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuDao.insertObject(entity.getId(),menuIds);*/
        //3.返回结果
        return rows;
    }

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }
}
